<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 任务订单
 *
 * @icon fa fa-circle-o
 */
class Order extends Backend {

    /**
     * Order模型对象
     * @var \app\admin\model\Order
     */
    protected $model = null;

    public function _initialize() {
        parent::_initialize();
        $this->model = model('Order');
        $this->view->assign("orderStatusList", $this->model->getOrderStatusList());
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function index() {
        $this->iAdminId;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->where($where)
                    ->field('fa_order.*,fa_user.phone,fa_task.task_title,fa_task.task_price,fa_position.position_name')
                    ->join('fa_user', 'fa_user.user_id = fa_order.user_id', 'left')
                    ->join('fa_task', 'fa_task.task_id = fa_order.task_id', 'left')
                    ->join('fa_position', 'fa_position.position_id = fa_order.position_id', 'left')
                    ->join('fa_business', 'fa_business.admin_id = fa_task.admin_id', 'left')
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }

        return $this->view->fetch();
    }

    public function detail() {
        $oid = input("ids", 0);

        $data = $this->model
                ->where('fa_order.order_id', $oid)
                ->field('fa_order.*,fa_user.phone,fa_task.task_title,fa_task.show_time,fa_task.task_no,fa_task.task_price,fa_position.position_name')
                ->join('fa_user', 'fa_user.user_id = fa_order.user_id', 'left')
                ->join('fa_task', 'fa_task.task_id = fa_order.task_id', 'left')
                ->join('fa_position', 'fa_position.position_id = fa_order.position_id', 'left')
                ->join('fa_business', 'fa_business.admin_id = fa_task.admin_id', 'left')
                ->find();

        if ($data) {
            $data['status_text'] = $this->model->getOrderStatusTextAttr($data['order_status'], $data);

            $carInfo = \think\Db::table('fa_user_car')
                    ->alias('uc')
                    ->field('uc.*,cb.brand_name,cc.color_name,ct.type_name,u.province,u.city,u.area')
                    ->join('fa_user u', 'uc.user_id = u.user_id')
                    ->join('fa_car_brand cb', 'uc.car_brand_id = cb.car_brand_id')
                    ->join('fa_car_color cc', 'cc.car_color_id = cc.car_color_id')
                    ->join('fa_car_type ct', 'ct.car_type_id = ct.car_type_id')
                    ->where(array('uc.user_id' => $data['user_id']))
                    ->find();

            $this->assign('car_info', $carInfo);
        }

        $tempo_list = \think\Db::table('fa_order_tempo')
                ->alias('ot')
                ->field('ot.*')
                ->join('fa_order o', 'o.order_id = ot.order_id')
                ->where('o.user_id', $data['user_id'])
                ->where('o.order_id', $data['order_id'])
                ->order('createtime', 'desc')
                ->select();

        foreach ($tempo_list as &$val) {
            $val['img_list'] = explode(',', $val['img_list']);
        }
        unset($val);

        $this->assign('list', $tempo_list);
        $this->assign('data', $data);
        return $this->view->fetch();
    }

    public function checkok() {
        $tempo_id = input('tempo_id', 0, 'intval');
        $data['status'] = 10;
        $data['updatetime'] = time();
        \think\Db::name('order_tempo')->where(['tempo_id' => $tempo_id])->update($data);

        $order_id = \think\Db::name('order_tempo')->where(['tempo_id' => $tempo_id])->value('order_id');

        $order_data['order_status'] = 13;
        $order_data['updatetime'] = time();
        \think\Db::name('order')->where(['order_id' => $order_id])->update($order_data);
        $this->success();
    }

    public function checkcancel() {
        $tempo_id = input('tempo_id', 0, 'intval');
        $refusal_reason = input('refusal_reason');
        $data['updatetime'] = time();
        $data['refusaltime'] = time();
        $data['refusal_reason'] = $refusal_reason;
        $data['status'] = 20;
        \think\Db::name('order_tempo')->where(['tempo_id' => $tempo_id])->update($data);

        $order_id = \think\Db::name('order_tempo')->where(['tempo_id' => $tempo_id])->value('order_id');

        $order_data['order_status'] = 12;
        $order_data['updatetime'] = time();
        \think\Db::name('order')->where(['order_id' => $order_id])->update($order_data);
        $this->success();
    }

}
